/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Objetos.Compra;
import DAO.AccessDatabase;
import Objetos.Usuario;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author celiobj
 */
public class RepositorioCompra implements RepositorioCompraInterface {

    @Override
    public void inserir(Compra compra,Usuario usu) {

        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `compras` (`idfornecedor`,`nota`, `valor`, `data`) VALUES ('" + compra.getCodFornecedor() + "','" + compra.getNota() + "', '" + compra.getValor() + "', '" + compra.getData() + "');");
        } catch (SQLException ex) {
            Logger.getLogger(RepositorioUsuario.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

    @Override
    public String pegarUltima(Usuario usu) {

        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT round(MAX(idcompra),2) FROM compras");
            rs.next();
            if (rs.getRow() == 0) {
                con.close();
                return null;
            } else {

                Compra compra = new Compra();
                compra.setCodCompra(rs.getString("round(MAX(idcompra),2)"));
                con.close();
                return compra.getCodCompra();
            }
        } catch (SQLException t) {
            System.out.println(t.getMessage());
            return null;

        }

    }

}
